package com.ruoyi.teach.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.teach.domain.TeachStudentTag;
import com.ruoyi.teach.event.EventPublisher;
import com.ruoyi.teach.service.ITeachStudentTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teach.mapper.TeachStudentMapper;
import com.ruoyi.teach.domain.TeachStudent;
import com.ruoyi.teach.service.ITeachStudentService;

/**
 * 学生信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-15
 */
@Service
public class TeachStudentServiceImpl implements ITeachStudentService
{
    @Autowired
    private TeachStudentMapper teachStudentMapper;

    @Autowired
    private ITeachStudentTagService teachStudentTagService;

    @Autowired
    private EventPublisher eventPublisher;

    /**
     * 查询学生信息
     *
     * @param studentId 学生信息主键
     * @return 学生信息
     */
    @Override
    public TeachStudent selectTeachStudentByStudentId(Long studentId)
    {
        TeachStudent teachStudent = teachStudentMapper.selectTeachStudentByStudentId(studentId);
        List<TeachStudentTag> teachStudentTags = teachStudentTagService.selectTeachStudentTagStudentId(studentId);
        ArrayList<String> stringsTag = new ArrayList<>();
        teachStudentTags.forEach(teachStudentTag -> {
            stringsTag.add(teachStudentTag.getTagName());
        });
        teachStudent.setStudentTags(stringsTag.toArray(new String[0]));
        return teachStudent;
    }

    /**
     * 查询学生信息列表
     *
     * @param teachStudent 学生信息
     * @return 学生信息
     */
    @Override
    public List<TeachStudent> selectTeachStudentList(TeachStudent teachStudent)
    {
        return teachStudentMapper.selectTeachStudentList(teachStudent);
    }

    @Override
    public List<TeachStudent> selectAllStudentList()
    {
        return teachStudentMapper.selectAllStudentList();
    }

    /**
     * 新增学生信息
     *
     * @param teachStudent 学生信息
     * @return 结果
     */
    @Override
    public int insertTeachStudent(TeachStudent teachStudent)
    {
        teachStudent.setCreateTime(DateUtils.getNowDate());
        teachStudent.setAvatar(StringUtils.getStringAvatar(teachStudent.getAvatar()));
        insertTeachStudentTag(teachStudent);
        return teachStudentMapper.insertTeachStudent(teachStudent);
    }

    //为学生添加标签
    private void insertTeachStudentTag(TeachStudent teachStudent) {
        TeachStudentTag studentTag = new TeachStudentTag();
        String[] studentTags = teachStudent.getStudentTags();
        if (studentTags != null) {
            for (String tag : studentTags) {
                studentTag.setTagName(tag);
                String id = UUID.randomUUID().toString();
                studentTag.setTagId(id.substring(0, 11));
                studentTag.setStudentId(teachStudent.getStudentId());
                studentTag.setStudentName(teachStudent.getStudentName());
                teachStudentTagService.insertTeachStudentTag(studentTag);
            }
        }
    }

    @Override
    public int updateStudentAvatar(String studentId, String avatar){
        TeachStudent teachStudent = new TeachStudent();
        teachStudent.setAvatar(avatar);
        teachStudent.setStudentId(Long.valueOf(studentId));
        return teachStudentMapper.updateTeachStudent(teachStudent);
    }

    /**
     * 学生信息已更新
     * 发送事件
     */
    public void eventSent(){
        eventPublisher.doStuffAndPublishAnEvent("student info is update event");
    }

    public void eventSentStudentInfo(TeachStudent teachStudent){
        eventPublisher.doStuffAndPublishAnEvent("student info",teachStudent);
    }

    /**
     * 修改学生信息
     *
     * @param teachStudent 学生信息
     * @return 结果
     */
    @Override
    public int updateTeachStudent(TeachStudent teachStudent)
    {
        teachStudent.setUpdateTime(DateUtils.getNowDate());
//        eventSent();
        eventSentStudentInfo(teachStudent);
        return teachStudentMapper.updateTeachStudent(teachStudent);
    }

    /**
     * 批量删除学生信息
     *
     * @param studentIds 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteTeachStudentByStudentIds(Long[] studentIds)
    {
        return teachStudentMapper.deleteTeachStudentByStudentIds(studentIds);
    }

    /**
     * 删除学生信息信息
     *
     * @param studentId 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteTeachStudentByStudentId(Long studentId)
    {
        return teachStudentMapper.deleteTeachStudentByStudentId(studentId);
    }
}
